package lc.q1_50;

public class Q42 {
    public int trap(int[] height) {
        // 从当前位置往左往右数
        // 可以弄两个指针，
        int result = 0;
        if (height.length < 1) {
            return 0;
        }
        int[] leftMax = new int[height.length];
        leftMax[0] = height[0];
        int[] rightMax = new int[height.length];
        rightMax[height.length - 1] = height[height.length - 1];
        for (int i = 0;i < height.length;i ++) {
            if (i > 0) {
                leftMax[i] = Math.max(leftMax[i-1], height[i]);
            }
            if (i < height.length - 1) {
                rightMax[height.length - i - 2] = Math.max(rightMax[height.length - i - 1], height[height.length-i-2]);
            }
        }
        for (int i = 0;i < height.length;i ++) {
            result += (Math.min(leftMax[i], rightMax[i]) - height[i]);
        }

        return result;
    }

}
